/*员工管理系统

  创建人：王鸿君  
  创建日期：2021/12/13/09：44
  修改日期：2021/12/13

  开发日志:
  1.用epoll实现并发服务器
  2.管理员修改函数，应加入工号判断，如果数据库中没有工号应该返回修改失败
  3.
  错误日志：
  1.

*/

#include "head.h"
#include <time.h>
#define NAME_MAX1 32
#define MSG_MAX 256
#define ADDR_MAX 256
#define TEXT_MAX 2048
char global[2048];
typedef struct staffinfo  //封装员工信息
{
	int usertype;   //员工权限
	char name[NAME_MAX1];
	int age;
	int staffnum;
	char addr[ADDR_MAX];
	char passwd[ADDR_MAX];
	char phone[ADDR_MAX];
	char work[ADDR_MAX];
	char date[ADDR_MAX];
	int level;    //员工职级
	float salary;   //薪资
}stinfo;

typedef struct messager  //封装消息结构体
{
	char type;
	char text[TEXT_MAX];
	stinfo info;
}MSG;
void insert_history(char *str);
int main(int argc, const char *argv[])
{
	sqlite3 *pdb = NULL;
	if(sqlite3_open("./staff.db",&pdb) != 0)
	{
		perror("open pdb err.");
		return -1;
	}
	char *errmsg;
	char sql22[512] = "create table history(text char);";
	char sql[512] = "create table staff(staffnum int primary key,name char primary key,age int,usertype int,addr char,passwd char,phone char,work char,date char,level int,salary float);";
	if(sqlite3_exec(pdb,sql,NULL,NULL,&errmsg)< 0)
	{
		perror("sqlite3 tables.");
	}
	if(sqlite3_exec(pdb,sql22,NULL,NULL,&errmsg)<0)
	{
		perror("sqlite3 history create err.");
	}
	sqlite3_close(pdb);
	printf("系统数据库启动成功!\n");
	int sockfd,acceptfd;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
		perror("sockfd err.");
		return -1;
	}

	struct sockaddr_in serveraddr;
	struct sockaddr_in clientaddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

	socklen_t len1 = sizeof(serveraddr);
	socklen_t len2 = sizeof(clientaddr);

	if(bind(sockfd,(struct sockaddr*)&serveraddr,len1)<0)
	{
		perror("bind err.");
		return -1;
	}

	if(listen(sockfd,10)<0)
	{
		perror("listen err.");
		return -1;
	}
	//用epoll实现并发服务器
	int epfd = epoll_create(1);
	struct epoll_event event;
	struct epoll_event events[30];

	event.data.fd = sockfd;
	event.events = EPOLLIN | EPOLLET;
	epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
	int i,readfd;
	while(1)
	{
		readfd = epoll_wait(epfd,events,30,-1);
		if(readfd < 0)
		{
			perror("epoll wait err.");
			return -1;
		}
		for(i = 0;i<readfd;i++)
		{
			//监听客户端链接
			if(events[i].data.fd == sockfd)
			{
				acceptfd = accept(sockfd,(struct sockaddr*)&clientaddr,&len2);
				if(acceptfd < 0)
				{
					perror("accept err.");
					return -1;
				}
				printf("client.ip: %s,port :%d\n",(char *)inet_ntoa(clientaddr.sin_addr.s_addr),\
						ntohs(clientaddr.sin_port));
				printf("员工连接!\n");
				/*******************************************   发送函数体    *****************************************************/
				/*  user code */


				MSG msg2;
				msg2.type = 'S'; //消息类型为服务器类型'Ｓ'
				memset(msg2.text,0,TEXT_MAX);

				/*  user code */
				event.data.fd = acceptfd;
				event.events = EPOLLIN | EPOLLET;
				epoll_ctl(epfd,EPOLL_CTL_ADD,acceptfd,&event);
			}
			//并发接收
			else
			{
				MSG msg;
				ssize_t recvbyte;
				recvbyte = recv(events[i].data.fd,&msg,sizeof(MSG),0);
				if(recvbyte < 0)
				{
					perror("recv err.");
					return -1;
				}
				else if(recvbyte == 0)
				{
					//客户端退出
					printf("员工退出系统\n");
					epoll_ctl(epfd,EPOLL_CTL_DEL,events[i].data.fd,&event);
					close(events[i].data.fd);

				}
				else
				{
					int val;
					/* user code */
					switch(msg.type)
					{
						//**************************   管理员登录  *************************
					case 'L':
						{
							MSG msg_L;
							msg_L.type = 'L';
							printf("client login\n");
							val = func_L_log(msg.info.name,msg.info.passwd);
							if(val == 1)//登录失败
							{
								printf("登录失败\n");
								msg_L.info.age = 1;
								send(events[i].data.fd,&msg_L,sizeof(MSG),0);
							}
							else if(val == 0)//登录成功
							{
								char buf[512] = "";
								sprintf(buf,"管理员%s登录系统成功",msg.info.name);
								insert_history(buf);
								printf("%s\n",buf);
								msg_L.info.age = 0;
								send(events[i].data.fd,&msg_L,sizeof(MSG),0);
							}
							break;
						}
						//*************************     管理员添加   *************************
					case 'R':
						{
							MSG msg_r;
							char *errmsg;
							msg_r.type = 'R';
							msg_r.info.age = 1;
							char sql_r[512] = "";
							char buf[256] = "";
							sprintf(sql_r,"insert into staff values(%d,\"%s\",%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d,%f);",msg.info.staffnum,msg.info.name,msg.info.age,msg.info.usertype,msg.info.addr,msg.info.passwd,msg.info.phone,msg.info.work,msg.info.date,msg.info.level,msg.info.salary);
							if(sqlite3_exec(pdb,sql_r,NULL,NULL,&errmsg)!= 0)
							{
								perror("insert into err.");
							}
							sprintf(buf,"管理员%s添加了一个成员%s",msg.text,msg.info.name);
							insert_history(buf);
							printf("%s\n",buf);
							send(events[i].data.fd,&msg_r,sizeof(MSG),0);
							break;
						}
						//******************  管理员全部查询 ********************
					case 'a':
						{
							sqlite3 *pdb_a = NULL;
							if(sqlite3_open("./staff.db",&pdb_a) != 0)
							{
								perror("select all sqlite3 pdb open err.");
							}							
							MSG msg_a;
							msg_a.type = 'a';
							char sql_a[128] = "";
							char *errmsg;
							char **result;
							int nrow,ncolumn;
							char buf[256] = "";
							sprintf(buf,"管理员%s查询全部成员",msg.info.name);
							insert_history(buf);
							printf("%s\n",buf);

							sprintf(sql,"select * from staff;");
							if(sqlite3_get_table(pdb_a,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
							{
								perror("search ep err");
								return -1;
							}
							if(nrow == 0)
							{
								printf("nrow == 0\n");
								return -1;
							}

							int k,n = 1,j = 0;
							int num = ncolumn;
							printf("发送查询结果\n");
							//将结果数组中的每一个成员发送
							for(k = 1;k<=ncolumn;k++)
							{
								for(j = 0;j<nrow;j++)
								{
									send(events[i].data.fd,result[num++],ADDR_MAX,0);

								}
							}
							send(events[i].data.fd,"###",ADDR_MAX,0);
							sqlite3_close(pdb_a);
							break;
						}
						//*************      普通用户查询        **********************
					case 's':
						{
							MSG msg_ssend;
							char *errmsg;
							char **result;
							int nrow,ncolumn;
							msg_ssend.type = 's';
							char sql[512] = "";
							char buf[512] = "";
							sprintf(buf,"普通用户%s查询信息",msg.info.name);
							insert_history(buf);
							printf("%s\n",buf);
							sqlite3 *pdb_s = NULL;
							if(sqlite3_open("./staff.db",&pdb_s) != 0)
							{
								perror("pdb_s open err.");
							}
							sprintf(sql,"select * from staff where name = \"%s\";",msg.info.name);
							if(sqlite3_get_table(pdb_s,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
							{
								perror("search ep err");
								return -1;
							}
							fflush(stdout);
							if(nrow == 0)
							{
								printf("nrow == 0\n");
								return -1;
							}
							int num_s,r,e;
							num_s = ncolumn;
							for(r = 1;r<=ncolumn;r++)
							{
								for(e = 0;e<nrow;e++)
								{
									send(events[i].data.fd,result[num_s++],ADDR_MAX,0);
								}
							}
							fflush(stdout);
							fflush(stdin);
							send(events[i].data.fd,"###",ADDR_MAX,0);

							break;
						}

						//*************************    管理员修改    *************************
					case 'M':
						{
							char *errmsg;
							int flag = 0;
							char **result;
							int nrow,ncolumn;
							char sql1[512] = "";
							printf("%d\n",msg.info.staffnum);
							sprintf(sql1,"select * from staff where staffnum = %d;",msg.info.staffnum);
							sqlite3 *pdb_m = NULL;
							if(sqlite3_open("./staff.db",&pdb_m) != 0)
							{
								perror("pdf_m open err.");
							}
							if(sqlite3_get_table(pdb_m,sql1,&result,&nrow,&ncolumn,&errmsg)!=SQLITE_OK)
							{
								perror("管理员修改工号查无此人!\n");
							}
							if(nrow ==  0)
							{
								flag = 1;
								MSG msg_m_send1;
								msg_m_send1.type = 'M';
								msg_m_send1.info.age = 2;
								send(events[i].data.fd,&msg_m_send1,sizeof(MSG),0);
							}
							else
							{
								MSG msg_m_recv1;
								MSG msg_m_send2;
								msg_m_send2.info.age = 3;
								send(events[i].data.fd,&msg_m_send2,sizeof(MSG),0);
								recv(events[i].data.fd,&msg_m_recv1,sizeof(MSG),0);
								switch(msg_m_recv1.text[0])
								{
								case 'a':
									{

										char sql[512] = "";
										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号%d的姓名为%s",msg_m_recv1.info.addr,msg_m_recv1.info.staffnum,msg_m_recv1.info.name);
										sprintf(sql,"update staff set name = \"%s\" where staffnum = %d;",msg_m_recv1.info.name,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -a err.");
										}
										insert_history(buf);
										printf("%s\n",buf);
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'b':
									{
										char sql2[512] = "";
										char buf[512] = "";
										sprintf(sql2,"update staff set usertype = %d where staffnum = %d;",msg_m_recv1.info.usertype,msg_m_recv1.info.staffnum);
										sprintf(buf,"管理员%s修改工号%d的权限为%d",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.usertype);
										insert_history(buf);
										printf("%s\n",buf);
										if(sqlite3_exec(pdb_m,sql2,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -b err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'c':
									{
										char sql3[512] = "";
										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的电话为%s",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.phone);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql3,"update staff set phone = \"%s\" where staffnum = %d;",msg_m_recv1.info.phone,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql3,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -c err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);

										break;
									}
								case 'd':
									{
										char sql4[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的地址为%s",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.addr);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql4,"update staff set addr = \"%s\" where staffnum = %d;",msg_m_recv1.info.addr,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql4,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -d err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'e':
									{
										char sql5[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的年龄为%d",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.age);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql5,"update staff set age = %d where staffnum = %d;",msg_m_recv1.info.age,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql5,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -e err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'f':
									{
										char sql6[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的密码为%s",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.passwd);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql6,"update staff set passwd = \"%s\" where staffnum = %d;",msg_m_recv1.info.passwd,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql6,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -f err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'g':
									{
										char sql7[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的职位为%s",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.work);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql7,"update staff set work = \"%s\" where staffnum = %d;",msg_m_recv1.info.work,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql7,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -g err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'h':
									{
										char sql8[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的薪资为%f",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.salary);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql8,"update staff set salary = %f where staffnum = %d;",msg_m_recv1.info.salary,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql8,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -f err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								case 'i':
									{
										char sql9[512] = "";

										char buf[512] = "";
										sprintf(buf,"管理员%s修改工号为%d的职级为%d",msg_m_recv1.info.name,msg_m_recv1.info.staffnum,msg_m_recv1.info.level);
										insert_history(buf);
										printf("%s\n",buf);
										sprintf(sql9,"update staff set level = %d where staffnum = %d;",msg_m_recv1.info.level,msg_m_recv1.info.staffnum);
										if(sqlite3_exec(pdb_m,sql9,NULL,NULL,&errmsg)!= 0)
										{
											perror("modify -f err.");
										}
										printf("发送确认修改!\n");

										MSG msg_mm;
										msg_mm.type = 'm';
										msg_mm.info.age = 1;
										send(events[i].data.fd,&msg_mm,sizeof(MSG),0);
										break;
									}
								default:
									printf("modify err.\n");
									break;
								}


								sqlite3_close(pdb_m);
							}
							break;
						}
						//***********************    管理员删除    *********************
					case 'D':
						{
							MSG msg_dd;
							msg_dd.type = 'D';
							char *errmsg,**result;
							int ncolumn,nrow;
							sqlite3 *pdb_d = NULL;
							if(sqlite3_open("./staff.db",&pdb_d) != 0)
							{
								perror("pdb_m open err.");
							}
							//*****************判断数据库中是否有该用户*****
							char sql_d_if[512] = "";
							sprintf(sql_d_if,"select * from staff where name = \"%s\" and staffnum = %d",msg.info.name,msg.info.staffnum);
							if(sqlite3_get_table(pdb_d,sql_d_if,&result,&nrow,&ncolumn,&errmsg)!= SQLITE_OK)
							{
								perror("gettable err.");
								sqlite3_free(errmsg);
							}
							if(nrow == 0)
							{
								msg_dd.info.age = 2;
							}
							else
							{
								msg_dd.info.age = 1;
								//*****************删除操作***********************
								char sql_d[512] = "";
								sprintf(sql_d,"delete from staff where name = \"%s\" and staffnum = %d;",msg.info.name,msg.info.staffnum);
								if(sqlite3_exec(pdb_d,sql_d,NULL,NULL,&errmsg)!= 0)
								{
									perror("delete err.");
								}
								sqlite3_close(pdb_d);
								char buf[256] = "";
								sprintf(buf,"管理员%s删除了个工号为%d,姓名为%s的员工",msg.text,msg.info.staffnum,msg.info.name);
								insert_history(buf);
								printf("%s\n",buf);
							}
							send(events[i].data.fd,&msg_dd,sizeof(MSG),0);
							break;
						}
						//***************************   使用名字查询    ******************
					case 'c':
						{

							sqlite3 *pdb_c = NULL;
							if(sqlite3_open("./staff.db",&pdb_c)!=0)
							{
								perror("pdb_c open err.");
							}
							char buf[512];
							char *errmsg,**result;
							int ncolumn,nrow;
							MSG msg_cc;
							msg_cc.type = 'c';

							sprintf(buf,"管理员%s使用名字查询",msg.text);
							insert_history(buf);
							char sql[512] = "";
							sprintf(sql,"select * from staff where name = \"%s\";",msg.info.name);
							if(sqlite3_get_table(pdb_c,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
							{
								printf("names search err./");
								sqlite3_free(errmsg);
							}
							if(nrow == 0)
							{
								msg_cc.info.age = 2;
								perror("name search nrow = 0");
								send(events[i].data.fd,&msg_cc,sizeof(MSG),0);
							}
							else
							{
								msg_cc.info.age = 1;
								send(events[i].data.fd,&msg_cc,sizeof(MSG),0);
								int j,k,l;
								int num = ncolumn;
								for(j = 1;j<ncolumn;j++)
								{
									for(k = 0;k<nrow;k++)
									{
										send(events[i].data.fd,result[num++],256,0);
									}
								}
								send(events[i].data.fd,"!!!",256,0);
							}
							sqlite3_close(pdb_c);
							break;
						}
						//**************************   普通员工登录  *****************
					case 'l':
						{
							MSG msg_lrec;
							msg_lrec.type = 'l';
							sqlite3 *pdb = NULL;
							if(sqlite3_open("./staff.db",&pdb) != 0)
							{
								perror("func_L_log pdb open err.");
								return -1;
							}

							char *errmsg,**result;
							int ncolumn,nrow;
							char sql[512];
							char buf[256] = "";
							if(msg.info.name[strlen(msg.info.name)] == '\n')
								msg.info.name[strlen(msg.info.name)] == '\0';
							if(msg.info.passwd[strlen(msg.info.passwd)] == '\n')
								msg.info.passwd[strlen(msg.info.passwd)] == '\0';
							sprintf(sql,"select * from staff where name = \"%s\" and passwd = \"%s\" and usertype = 0;",msg.info.name,msg.info.passwd);
							if(sqlite3_get_table(pdb,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
							{
								printf("func_L_log get table err./");
								sqlite3_free(errmsg);
							}
							if(nrow == 0)
							{
								printf("普通员工登录失败\n");
								msg_lrec.info.age = 2;//登录失败
							}
							else
							{
								sprintf(buf,"普通员工%s登录成功",msg.info.name);
								insert_history(buf);
								printf("%s\n",buf);
								msg_lrec.info.age = 3 ;//登录成功
							}
							send(events[i].data.fd,&msg_lrec,sizeof(MSG),0);
							sqlite3_close(pdb);
							sqlite3_free_table(result);
							break;
						}

						//********************  普通员工修改  ******************
					case 'i':
						{
							char *errmsg;
							MSG msg_send;
							msg_send.type = 'i';
							msg_send.info.age = 4;
							sqlite3 *pdb_l = NULL;
							if(sqlite3_open("./staff.db",&pdb_l) != 0)
							{
								perror("pdf_l open err.");
							}
							switch(msg.text[0])
							{
							case 'a':
								{
									char sql9[512] = "";

									sprintf(sql9,"update staff set addr = \"%s\" where name = \"%s\";",msg.info.addr,msg.info.name);
									if(sqlite3_exec(pdb_l,sql9,NULL,NULL,&errmsg)!= 0)
									{
										perror("nmodify -a err.");
									}
									char buf[512] = "";
									sprintf(buf,"普通员工%s的将自己的地址修改为%s",msg.info.name,msg.info.addr);
									insert_history(buf);
									printf("%s\n",buf);
									break;
								}
							case 'b':
								{
									char sql10[512] = "";

									sprintf(sql10,"update staff set phone = \"%s\" where name = \"%s\";",msg.info.phone,msg.info.name);
									if(sqlite3_exec(pdb_l,sql10,NULL,NULL,&errmsg)!= 0)
									{
										perror("nmodify -b err.");
									}
									char buf[512] = "";
									sprintf(buf,"普通员工%s将自己的电话修改为%s",msg.info.name,msg.info.phone);
									insert_history(buf);
									printf("%s\n",buf);
									break;
								}
							case 'c':
								{
									char sql11[512] = "";

									sprintf(sql11,"update staff set passwd = \"%s\" where name = \"%s\";",msg.info.passwd,msg.info.name);
									if(sqlite3_exec(pdb_l,sql11,NULL,NULL,&errmsg)!= 0)
									{
										perror("nmodify -c err.");
									}
									char buf[512] = "";
									sprintf(buf,"普通员工%s将自己的密码修改为%s",msg.info.name,msg.info.passwd);
									insert_history(buf);
									printf("%s\n",buf);
									break;
								}
							default:
								break;
							}
							send(events[i].data.fd,&msg_send,sizeof(MSG),0);
							sqlite3_close(pdb_l);
							break;
						}
						//***********************      管理员查询历史       **********************
					case 'h':
						{
							char *errmsg,**result;
							int ncolumn,nrow;
							sqlite3 *pdb_h = NULL;
							if(sqlite3_open("./staff.db",&pdb_h) != 0)
							{
								perror("pdb_h open err.");
							}
							char sql[512] = "select * from history;";
							if(sqlite3_get_table(pdb_h,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
							{
								printf("history search err./");
								sqlite3_free(errmsg);
							}
							if(nrow == 0)
							{
								perror("history nrow = 0");
							}
							else
							{
								int j,k,l;
								int num = ncolumn;
								for(j = 1;j<=ncolumn;j++)
								{
									for(k = 0;k<nrow;k++)
									{
										send(events[i].data.fd,result[num++],256,0);
									}
								}
								send(events[i].data.fd,"!!!",256,0);	
							}
							char buf[512];
							sprintf(buf,"管理员%s查询了历史",msg.info.name);
							insert_history(buf);
							printf("%s\n",buf);
							sqlite3_close(pdb_h);
							break;

						}
					default:
						break;
					}

					/* user code */
				}
			}


		}
	}
	return 0;
}

int func_L_log(char *name,char *pass)
{
	sqlite3 *pdb = NULL;
	if(sqlite3_open("./staff.db",&pdb) != 0)
	{
		perror("func_L_log pdb open err.");
	}

	char *errmsg,**result;
	int ncolumn,nrow;
	char sql[512];
	if(name[strlen(name)] == '\n')
		name[strlen(name)] == '\0';
	if(pass[strlen(pass)] == '\n')
		pass[strlen(pass)] == '\0';
	sprintf(sql,"select * from staff where name = \"%s\" and passwd = \"%s\" and usertype = 1;",name,pass);
	if(sqlite3_get_table(pdb,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("func_L_log get table err./");
		sqlite3_free(errmsg);
	}
	if(nrow == 0)
	{
		return 1;//登录失败
	}
	else
	{
		return 0; //登录成功
	}
	sqlite3_close(pdb);
	sqlite3_free_table(result);
}

void insert_history(char *str)
{
	time_t t;
	struct tm* ptm;
	t = time(&t);
	ptm = localtime(&t);
	sqlite3 *pdb = NULL;
	char *errmsg;
	if(sqlite3_open("./staff.db",&pdb)!= 0)
	{
		perror("insert history pdb open err.");
	}
	char sql[512] = "";
	sprintf(sql,"insert into history values(\"%d-%d-%d %d:%d:%d :%s\");",ptm->tm_year+1900,\
			ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,\
			ptm->tm_min,ptm->tm_sec,str);
	if(sqlite3_exec(pdb,sql,NULL,NULL,&errmsg)!=0)
	{
		perror("insert err,");
	}
}
